import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getAllRolesListNoByTenant, getAllTenantList } from './user.api';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';

//订单列表
const statusFmt = ({ text }: { text: any }) => {
  const color = text == '2' ? 'red' : text == '1' ? 'green' : '';
  return color ? render.renderTag(render.renderDict(text, 'user_status'), color) : '';
};
//列表 - 用户信息 status_dictText
export const columns: BasicColumn[] = [
  {
    title: '状态',
    dataIndex: 'status',
    customRender: statusFmt,
    width: 55,
  },
  {
    title: '账号',
    dataIndex: 'username',
    width: 70,
  },
  {
    title: '姓名',
    dataIndex: 'realname',
    width: 60,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 50,
    customRender: ({ text }) => {
      return render.renderDict(text, 'sex');
    },
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 85,
  },
  {
    title: '所属部门',
    width: 100,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: '角色权限',
    width: 100,
    dataIndex: 'roleCodeTxt',
  },
  {
    title: '权限类型',
    dataIndex: 'authType',
    width: 80,
    customRender: ({ text }) => {
      if (!text) return '';
      let color = 'red';
      switch (text) {
        case 1:
          color = 'text-green-500';
          break;
        case 2:
          color = 'text-blue-500';
          break;
        case 3:
          color = 'text-red-500';
          break;
      }
      return render.renderColor(render.renderDict(text, 'authType'), color);
    },
  },
  {
    title: '最近登录',
    width: 90,
    dataIndex: 'loginDate',
    format: 'date|YYYY-MM-DD HH:mm',
  },
  {
    title: '操作时间',
    width: 90,
    dataIndex: 'updateTime',
    format: 'date|YYYY-MM-DD HH:mm',
  },
];

//列表 - 回收站
export const recycleColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: render.renderAvatar,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 80,
    sorter: true,
    customRender: ({ text }) => {
      return render.renderDict(text, 'sex');
    },
  },
];

//查询 - 表单
export const searchFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'username',
    component: 'JInput',
    colProps: { span: 10 },
  },
  {
    label: '真实名称',
    field: 'realname',
    component: 'Input',
    colProps: { span: 3 },
  },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    colProps: { span: 3 },
  },
  {
    label: '用户状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'user_status',
      placeholder: '请选择状态',
      stringToNumber: true,
    },
    colProps: { span: 3 },
  },
];

//表单 - 用户编辑
export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    colProps: { span: 8 },
    required: true,
    // dynamicDisabled: ({ values }) => {
    //   return !!values.id;
    // },
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'username', model, schema, true),
  },
  {
    label: '用户姓名',
    field: 'realname',
    required: true,
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '身份证号',
    field: 'citizenNo',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '所属部门',
    field: 'selecteddeparts',
    component: 'JSelectDept',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        //checkStrictly: true,
        checkable: true,
        multiple: false,
        defaultExpandLevel: 5,
        //defaultExpandAll: true,
        // fieldNames: {
        //   key: 'id',
        //   value: 'id',
        //   label: 'label',
        // },
        onSelect: (options, values) => {
          //console.info("selecteddeparts@@departIds=", formModel.departIds);
          const { updateSchema } = formActionType;
          //所属部门修改后更新负责部门下拉框数据
          updateSchema([
            {
              field: 'departIds',
              componentProps: { options },
            },
          ]);
          //所属部门修改后更新负责部门数据
          formModel.departIds && (formModel.departIds = formModel.departIds.filter((item) => values.value.indexOf(item) > -1));
        },
      };
    },
    colProps: { span: 16 },
  },
  {
    label: '收客人',
    field: 'selectedsales',
    component: 'JSelectDept',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        checkable: true,
        getCheckStrictly: true,
        multiple: true,
        defaultExpandLevel: 5,
        maxTagCount: 1,
        showButton: false,
        modalTitle: '选中部门下的用户并做为收客人',
        // fieldNames: {
        //   key: 'id',
        //   value: 'id',
        //   label: 'label',
        // },
        onSelect: (options, values) => {
          const { updateSchema } = formActionType;
          //所属部门修改后更新负责部门下拉框数据
          updateSchema([
            {
              field: 'salesIds',
              componentProps: { options },
            },
          ]);
          //所属部门修改后更新负责部门数据
          formModel.salesIds && (formModel.salesIds = formModel.salesIds.filter((item) => values.value.indexOf(item) > -1));
        },
      };
    },
    colProps: { span: 8 },
  },
  {
    field: 'selectedroles',
    label: '角色权限',
    component: 'TreeSelect',
    componentProps: {
      placeholder: '请选择',
      treeDefaultExpandedKeys: ['98608507967900000'],
      treeData: [],
      dropdownStyle: { 
        maxHeight: '500px',
        overflow: 'auto',
        width: '300px'
      },
      replaceFields: {
        label: 'roleName',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    colProps: { span: 8 },
  },
  {
    label: '权限类型',
    field: 'authType',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'authType',
      placeholder: '请选择',
      stringToNumber: true,
      onSelect: (values, options) => {
        //console.info(values, options);
      },
    },
    colProps: { span: 8 },
  },
  {
    label: '职务',
    field: 'post',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'job_level',
      placeholder: '',
      stringToNumber: false,
      getPopupContainer: () => document.body,
    },
    colProps: { span: 8 },
  },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    // dynamicRules: ({ model, schema }) => {
    //   return [
    //     { ...rules.duplicateCheckRule("sys_user", "phone", model, schema, true)[0] },
    //     { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "手机号码格式有误" }
    //   ];
    // },
    colProps: { span: 8 },
  },
  {
    label: '生日',
    field: 'birthday',
    component: 'DatePicker',
    colProps: { span: 8 },
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: '请选择性别',
      stringToNumber: true,
    },
    colProps: { span: 8 },
  },
  {
    label: '星座',
    field: 'zodiac',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '血型',
    field: 'bloodType',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '入职时间',
    field: 'entryTime',
    component: 'DatePicker',
    colProps: { span: 8 },
  },
  {
    label: '家属关系',
    field: 'position',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'relation',
      placeholder: '',
      stringToNumber: false,
    },
    colProps: { span: 8 },
  },
  {
    label: '紧急联系',
    field: 'familyPhone',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '推送编号',
    field: 'address',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '职责',
    field: 'memo',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '显示通讯录',
    field: 'bookstatus',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'dev_flag',
      placeholder: '',
      stringToNumber: true,
    },
    colProps: { span: 8 },
  },
  {
    label: '餐厅经理',
    field: 'isLogeSystem',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'yn',
      placeholder: '',
      stringToNumber: false,
    },
    colProps: { span: 8 },
  },
  {
    label: '系统分类',
    field: 'relTenantIds',
    component: 'ApiSelect',
    componentProps: {
      api: getAllTenantList,
      mode: 'multiple',
      numberToString: true,
      labelField: 'name',
      valueField: 'id',
    },
    colProps: { span: 22 },
  },
  {
    label: '部门权限',
    field: 'ddlxTenantIds',
    component: 'JDictSelectTag',
    colProps: { span: 22 },
  },
  {
    label: '推送部门',
    field: 'ddfsTenantIds',
    component: 'JDictSelectTag',
    colProps: { span: 22 },
  },
  {
    label: '当前租户',
    field: 'tenantId',
    component: 'Input',
    show: false,
  },
];

//修改密码
export const formPasswordSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    componentProps: { readOnly: true },
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '请输入登录密码',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
];

export const formAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: '代理人用户名',
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 10,
    },
  },
  {
    field: 'startTime',
    label: '代理开始时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择代理开始时间',
    },
  },
  {
    field: 'endTime',
    label: '代理结束时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择代理结束时间',
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

export const formQuitAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: '交接人员',
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 1,
    },
  },
  {
    field: 'startTime',
    label: '交接开始时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择交接开始时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'endTime',
    label: '交接结束时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择交接结束时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

//租户用户列表
export const userTenantColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '用户姓名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 120,
    customRender: render.renderAvatar,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: '部门',
    width: 150,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ text }) => {
      if (text === '1') {
        return '正常';
      } else if (text === '3') {
        return '审批中';
      } else {
        return '已拒绝';
      }
    },
  },
];

//用户租户搜索表单
export const userTenantFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'username',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '名字',
    field: 'realname',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: '请选择性别',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];
